package view;

import javax.swing.JFrame;
import javax.swing.JLabel;


import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.filechooser.FileFilter;

import services.DataCache;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class WorkSpaceSelection extends JFrame implements Runnable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6057970986730393107L;
	private JTextField textField;
	public WorkSpaceSelection() {
		setResizable(false);
		JLabel lblWorkspace = new JLabel("Workspace");
		
		textField = new JTextField();
		//textField.setText(DataCache.sWorkSpace);
		textField.setColumns(10);
		
		JButton btnBrowser = new JButton("Browser...");
		btnBrowser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FileFilter showDialog = new FileFilter(){
					public boolean accept(File file) {
						return file.isDirectory();
					}
					@Override
					public String getDescription() {
						return "Folder";
					}
				};

				JFileChooser oFolderDia = new JFileChooser();
				oFolderDia.setDialogTitle("Select workspace folder.");
				oFolderDia.setFileFilter(showDialog);
				oFolderDia.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				oFolderDia.setAcceptAllFileFilterUsed(false);
				int reVal = oFolderDia.showOpenDialog(WorkSpaceSelection.this);
				if (reVal == JFileChooser.APPROVE_OPTION)
				{
					textField.setText(oFolderDia.getSelectedFile().getAbsolutePath());
				}			
			}
		});
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(WorkSpaceSelection.class.getResource("/resources/Excel-icon.png")));
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!textField.getText().isEmpty()){
					String sWSFolder = textField.getText();
					File f = new File(sWSFolder);
					if (f.isDirectory())
					{
						//DataCache.sWorkSpace = sWSFolder;
					}
				}
				setVisible(false);
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnBrowser)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnOk))
						.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
						.addComponent(lblWorkspace, Alignment.LEADING))
					.addGap(6))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label, Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblWorkspace)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(41)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnOk)
								.addComponent(btnBrowser))))
					.addGap(130))
		);
		getContentPane().setLayout(groupLayout);
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		setSize(490,155);
		setVisible(true);
	}
	
}
